package juc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author fangbin
 * @desc 可重入锁演示打印2
 * @date 2020/5/30 8:04
 */
public class OutPuter {
    Lock lock = new ReentrantLock();
    public void output(String name){
        int len = name.length();
        //lock.lock();
        try {
            for (int i = 0; i < len; i++) {
                System.out.print(name.charAt(i));
            }
            System.out.println("");
        }finally {
          //  lock.unlock();
        }
    }

    public static void main(String[] args) {
        OutPuter outPuter = new OutPuter();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outPuter.output("房斌");
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outPuter.output("BingoSay");
                }
            }
        }).start();
    }
}
